package com.coldworks.coldjoke.activity;

import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.coldworks.base.BaseActivity;
import com.coldworks.base.manager.BasePrefManager;
import com.coldworks.base.util.BaseCONST;
import com.coldworks.coldjoke.R;
import com.coldworks.coldjoke.manager.UserManager;
import com.coldworks.coldjoke.manager.UserManager.UserStateSubject;
import com.coldworks.coldjoke.util.CONST;
import com.coldworks.coldjoke.util.RegularUtil;
import com.coldworks.coldjoke.util.UrlUtil;

public class LoginActivity extends BaseActivity implements Observer {

	private EditText emailText;
	private EditText pwdText;
	private Button loginButton;
	private Button registerBtn;
	private ImageButton backBtn;

	private String email;
	private String password;

	private UserStateSubject userStateSubject;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			setContentView(R.layout.activity_login);
			setUpView();
			initData();
			setUpListener();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setUpView() {
		emailText = (EditText) findViewById(R.id.input_email);
		pwdText = (EditText) findViewById(R.id.input_pwd);
		loginButton = (Button) findViewById(R.id.button_login);
		registerBtn = (Button) findViewById(R.id.button_register);
		backBtn = (ImageButton) findViewById(R.id.button_back);

	}

	private void initData() {
		userStateSubject = UserManager.getInstance().getUserStateSubject();
		userStateSubject.addObserver(this);
		email = BasePrefManager.getInstance().getStringFromPrefs(context,
				BaseCONST.KEY.USER_EMAIL, null);
		emailText.setText(email);
	}

	private void setUpListener() {

		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setResult(CONST.RESULT.RESULT_CANCELED);
				finish();
			}
		});

		registerBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(LoginActivity.this,
						RegisterActivity.class));
				overridePendingTransition(android.R.anim.fade_in,
						android.R.anim.fade_out);
				finish();
			}
		});

		loginButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!RegularUtil.checkEmailAdress(emailText.getText()
						.toString())) {
					Toast.makeText(context, R.string.login_no_email,
							Toast.LENGTH_LONG).show();
				} else if (!RegularUtil.checkPwd(pwdText.getText().toString())) {
					Toast.makeText(context, R.string.login_no_pwd,
							Toast.LENGTH_LONG).show();
				} else {
					email = emailText.getText().toString();
					password = pwdText.getText().toString();
					UserManager.getInstance().setUserEmail(email);
					UserManager.getInstance().loginTask(context,
							UrlUtil.getLoginUrl(email, password, "on"), false);
				}
			}
		});

	}

	/**
	 * LoginActivity也注册到userStateSubject
	 * 
	 * @param observable
	 * @param data
	 */
	@Override
	public void update(Observable o, Object data) {
		UserStateSubject observable = (UserStateSubject) o;
		if (observable.getUserState() == UserManager.LOGINED) {
			log.info("login notify");
			UserManager.getInstance().setUserEmail(email);
			UserManager.getInstance().setIsAutoLogin(true);
			UserManager.getInstance().setUserPassword(password);
			setResult(CONST.RESULT.RESULT_OK);
			finish();
		} else {
			log.info("logout notify");
			UserManager.getInstance().setUserPassword(null);
			pwdText.getText().clear();
		}

	}

}
